#include <project.h>
#include "userMain.h"
#include "userUartRxCtrl.h"
#define     UART_ON

void userUartInitFunc(void)
{
    #ifdef UART_ON
    UART_SetCustomInterruptHandler(&userUartRxInterrupHandler); 
    UART_Start();
    #endif
}

void userUartRxInterrupHandler(void)
{ //Uart Receive data from 8266
   #ifdef UART_ON
    unsigned char userUartRxData;
    if(0u !=( UART_GetRxInterruptSourceMasked()& UART_INTR_RX_NOT_EMPTY))
   {
        UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY );  
        userUartRxData = (uint8)UART_UartGetByte();
        userUartRxBufferPush(userUartRxData);
   } 
   #endif
}

unsigned char userAlarmFlag = 0;
unsigned char userCurrentNetState = 0;
unsigned char userRxBuff[10];
extern circle_buffer userRxBuffer;
extern uint8 userStartFlag;
extern uint8 userLedState;
void userUartRxCtrlFunc(void)
{
    #ifdef UART_ON
    unsigned char uRxLen = 0;
    unsigned char userErrorFlag;
    if(userUartRxIdle()==0) return ;
    userGetRxBufferLen(&uRxLen);
    if(uRxLen==0) return ;
    userRxBufferPop(&userRxBuff[0]);
    switch(userRxBuff[0])
    {
        case 0xC1:
        {
            userRxMemcpy(userRxBuff,5);
            userUartRxDataCheckFunc(&userErrorFlag);
            break;
        }
        case 0x63:
        {
            userRxMemcpy(userRxBuff,7);
            userUartRxDataCheckFunc(&userErrorFlag);
            break;
        }
        default:
        {
            userErrorFlag = 1;
            break;
        }
    }
    if(userErrorFlag)   
    {
        userRxBufferReset();
        return ;
    }
    userUartRxProcesFunc();
    #endif
}

void userUartRxProcesFunc(void)
{
    if(userRxBuff[0] == 0xC1)
    {
        if(userRxBuff[1]!= 5)return ;
        userCurrentNetState = userRxBuff[3];
        return ;
    }
    if(userRxBuff[0] != 0x63)
        return ;
    if(userRxBuff[1]!= 7)return ;
    switch(userRxBuff[3])
    {
        case 0x01:
        {
            userRelayCtrlFunc(1,userRxBuff[5]);       //Relay control
            userSendNodeRegisterInfoFunc();           //userSendRelayInfoFunc();
            break;
        }
        case 0x03:
        {
            userBeepCtrlFunc(userRxBuff[4],userRxBuff[5]);    //Beep control
            userSendNodeRegisterInfoFunc();
            break;
        }
        case 0x04:
        {
            userLedCtrlFunc(userRxBuff[4],userRxBuff[5]);   //Back LED control
            userSendNodeRegisterInfoFunc();
            //userSendLedInfoFunc();
            break;
        }
        case 0x05:
        {
            userNightCtrlFunc(userRxBuff[4],userRxBuff[5]); //Night Control
            //userSendNightInfoFunc();
            userSendNodeRegisterInfoFunc();
            break;
        }
        case 0x07:
        {
            //userSendMotionInfoFunc();
            userMotionCtrlFunc(userRxBuff[5]);
            //userMotionSensitivitySet(userRxBuff[4],userRxBuff[5]);
            userSendNodeRegisterInfoFunc();
            break;
        }
        case 0x0C:
        {
            userEnableInfoCtrlFunc(userRxBuff[4],userRxBuff[5]); //Enable config Control
            //userSendEnableInfoFunc();
            userSendNodeRegisterInfoFunc();
            break;
        }
        case 0x0E:
        {
            userFactoryResetFunc();
            userSendNodeRegisterInfoFunc();
            CyDelay(15);
            break;
        }
        case 0x0F:
        {
            userSensorConfigSetFunc(userRxBuff[4],userRxBuff[5]);
            userSendNodeRegisterInfoFunc();
            break;
        }
        case 0x2B:
        {
            if(userRxBuff[5])
            {
                userAlarmFlag = 1;
            }
            else
            {
                userAlarmFlag = 0;
                LED_Write(userLedState);
                userBeepOff();
            }
            userSendNodeRegisterInfoFunc();
            break;
        }
        case 0x10:
        {
            userSendNodeRegisterInfoFunc();
            userStartFlag = 0;
            break;
        }
        default:
        {
            break;
        }
    }
}

void userUartRxDataCheckFunc(unsigned char* uErrorFlag)
{
    #ifdef UART_ON
    unsigned char user_i,userCheckSum=0;
    unsigned char userRxDataLength;
    userRxDataLength = userRxBuff[1];
    for(user_i=0;user_i<(userRxDataLength-1);user_i++)
    {
       userCheckSum += userRxBuff[user_i]; 
    }
    if(userCheckSum == userRxBuff[userRxDataLength-1])
    {
         *uErrorFlag = 0;
    }
    else
    {
         *uErrorFlag = 1;  
    }
    #endif
}